<?php

class Core_Model_Design_Block extends Dcmp_Model_Entity {

    public function __construct() {
        parent::__construct();
    }

    protected function getTableClass() {
        return Kd_Loader::loadClass('table_design_block');
    }

    protected function beforeInsert($transaction) {
        $sorting = $this->getTable()->getMaxSorting($this->getPageTemplateId(), $this->getPosition());
        $sorting = empty($sorting)? 1: $sorting + 1;
        $this->setSorting();
        parent::beforeInsert($transaction);
    }

    protected function initializeTable($table) {
        $table->setName('{t}design_block');
        $table->createColumn('id', 'guid', TRUE);
        $table->createColumn('name', 'string');
        $table->createColumn('position', 'string');
        $table->createColumn('enabled', 'integer');
        $table->createColumn('parameters', 'serialize');
        $table->createColumn('module_id', 'string');
        $table->createColumn('area_id', 'string');
        $table->createColumn('block_template_id', 'string');
        $table->createColumn('page_template_id', 'string');
        $table->createColumn('sorting', 'string');
    }

    public function loadByCode($code) {
        $row = $this->getTable()->getByCode($code);
        $this->fill($row);
        return $this;
    }

}
